/*
 * Copyright (c) 2008-2010,2012-2014 Apple Inc. All Rights Reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */


#include <CoreFoundation/CoreFoundation.h>
#include <Security/SecCertificate.h>
#include <Security/SecItem.h>
#include <Security/SecItemPriv.h>
#include <Security/SecBase.h>
#include <stdlib.h>
#include <unistd.h>

#include "Security_regressions.h"

#define CFReleaseNull(CF) { CFTypeRef _cf = (CF); if (_cf) {  (CF) = NULL; CFRelease(_cf); } }

/*
	subject= /O=VeriSign Trust Network/OU=VeriSign, Inc./OU=VeriSign International Server CA - Class 3/OU=www.verisign.com/CPS Incorp.by Ref. LIABILITY LTD.(c)97 VeriSign
	issuer= /C=US/O=VeriSign, Inc./OU=Class 3 Public Primary Certification Authority
	serial=254B8A853842CCE358F8C5DDAE226EA4
*/
const uint8_t _myCert[] = {
    0x30, 0x82, 0x03, 0x83, 0x30, 0x82, 0x02, 0xec,
    0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x10, 0x25,
    0x4b, 0x8a, 0x85, 0x38, 0x42, 0xcc, 0xe3, 0x58,
    0xf8, 0xc5, 0xdd, 0xae, 0x22, 0x6e, 0xa4, 0x30,
    0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
    0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x5f,
    0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
    0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x17, 0x30,
    0x15, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0e,
    0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e,
    0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x37,
    0x30, 0x35, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13,
    0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x20, 0x33,
    0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20,
    0x50, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x20,
    0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63,
    0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x41, 0x75,
    0x74, 0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x30,
    0x1e, 0x17, 0x0d, 0x39, 0x37, 0x30, 0x34, 0x31,
    0x37, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a,
    0x17, 0x0d, 0x31, 0x31, 0x31, 0x30, 0x32, 0x34,
    0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5a, 0x30,
    0x81, 0xba, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
    0x55, 0x04, 0x0a, 0x13, 0x16, 0x56, 0x65, 0x72,
    0x69, 0x53, 0x69, 0x67, 0x6e, 0x20, 0x54, 0x72,
    0x75, 0x73, 0x74, 0x20, 0x4e, 0x65, 0x74, 0x77,
    0x6f, 0x72, 0x6b, 0x31, 0x17, 0x30, 0x15, 0x06,
    0x03, 0x55, 0x04, 0x0b, 0x13, 0x0e, 0x56, 0x65,
    0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x2c, 0x20,
    0x49, 0x6e, 0x63, 0x2e, 0x31, 0x33, 0x30, 0x31,
    0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x2a, 0x56,
    0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x20,
    0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x74,
    0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65,
    0x72, 0x76, 0x65, 0x72, 0x20, 0x43, 0x41, 0x20,
    0x2d, 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x20,
    0x33, 0x31, 0x49, 0x30, 0x47, 0x06, 0x03, 0x55,
    0x04, 0x0b, 0x13, 0x40, 0x77, 0x77, 0x77, 0x2e,
    0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67, 0x6e,
    0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x43, 0x50, 0x53,
    0x20, 0x49, 0x6e, 0x63, 0x6f, 0x72, 0x70, 0x2e,
    0x62, 0x79, 0x20, 0x52, 0x65, 0x66, 0x2e, 0x20,
    0x4c, 0x49, 0x41, 0x42, 0x49, 0x4c, 0x49, 0x54,
    0x59, 0x20, 0x4c, 0x54, 0x44, 0x2e, 0x28, 0x63,
    0x29, 0x39, 0x37, 0x20, 0x56, 0x65, 0x72, 0x69,
    0x53, 0x69, 0x67, 0x6e, 0x30, 0x81, 0x9f, 0x30,
    0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
    0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81,
    0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81,
    0x00, 0xd8, 0x82, 0x80, 0xe8, 0xd6, 0x19, 0x02,
    0x7d, 0x1f, 0x85, 0x18, 0x39, 0x25, 0xa2, 0x65,
    0x2b, 0xe1, 0xbf, 0xd4, 0x05, 0xd3, 0xbc, 0xe6,
    0x36, 0x3b, 0xaa, 0xf0, 0x4c, 0x6c, 0x5b, 0xb6,
    0xe7, 0xaa, 0x3c, 0x73, 0x45, 0x55, 0xb2, 0xf1,
    0xbd, 0xea, 0x97, 0x42, 0xed, 0x9a, 0x34, 0x0a,
    0x15, 0xd4, 0xa9, 0x5c, 0xf5, 0x40, 0x25, 0xdd,
    0xd9, 0x07, 0xc1, 0x32, 0xb2, 0x75, 0x6c, 0xc4,
    0xca, 0xbb, 0xa3, 0xfe, 0x56, 0x27, 0x71, 0x43,
    0xaa, 0x63, 0xf5, 0x30, 0x3e, 0x93, 0x28, 0xe5,
    0xfa, 0xf1, 0x09, 0x3b, 0xf3, 0xb7, 0x4d, 0x4e,
    0x39, 0xf7, 0x5c, 0x49, 0x5a, 0xb8, 0xc1, 0x1d,
    0xd3, 0xb2, 0x8a, 0xfe, 0x70, 0x30, 0x95, 0x42,
    0xcb, 0xfe, 0x2b, 0x51, 0x8b, 0x5a, 0x3c, 0x3a,
    0xf9, 0x22, 0x4f, 0x90, 0xb2, 0x02, 0xa7, 0x53,
    0x9c, 0x4f, 0x34, 0xe7, 0xab, 0x04, 0xb2, 0x7b,
    0x6f, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x81,
    0xe3, 0x30, 0x81, 0xe0, 0x30, 0x0f, 0x06, 0x03,
    0x55, 0x1d, 0x13, 0x04, 0x08, 0x30, 0x06, 0x01,
    0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x44, 0x06,
    0x03, 0x55, 0x1d, 0x20, 0x04, 0x3d, 0x30, 0x3b,
    0x30, 0x39, 0x06, 0x0b, 0x60, 0x86, 0x48, 0x01,
    0x86, 0xf8, 0x45, 0x01, 0x07, 0x01, 0x01, 0x30,
    0x2a, 0x30, 0x28, 0x06, 0x08, 0x2b, 0x06, 0x01,
    0x05, 0x05, 0x07, 0x02, 0x01, 0x16, 0x1c, 0x68,
    0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77,
    0x77, 0x77, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x73,
    0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
    0x43, 0x50, 0x53, 0x30, 0x34, 0x06, 0x03, 0x55,
    0x1d, 0x25, 0x04, 0x2d, 0x30, 0x2b, 0x06, 0x08,
    0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01,
    0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,
    0x03, 0x02, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
    0x86, 0xf8, 0x42, 0x04, 0x01, 0x06, 0x0a, 0x60,
    0x86, 0x48, 0x01, 0x86, 0xf8, 0x45, 0x01, 0x08,
    0x01, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f,
    0x04, 0x04, 0x03, 0x02, 0x01, 0x06, 0x30, 0x11,
    0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8,
    0x42, 0x01, 0x01, 0x04, 0x04, 0x03, 0x02, 0x01,
    0x06, 0x30, 0x31, 0x06, 0x03, 0x55, 0x1d, 0x1f,
    0x04, 0x2a, 0x30, 0x28, 0x30, 0x26, 0xa0, 0x24,
    0xa0, 0x22, 0x86, 0x20, 0x68, 0x74, 0x74, 0x70,
    0x3a, 0x2f, 0x2f, 0x63, 0x72, 0x6c, 0x2e, 0x76,
    0x65, 0x72, 0x69, 0x73, 0x69, 0x67, 0x6e, 0x2e,
    0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x63, 0x61, 0x33,
    0x2e, 0x63, 0x72, 0x6c, 0x30, 0x0d, 0x06, 0x09,
    0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
    0x05, 0x05, 0x00, 0x03, 0x81, 0x81, 0x00, 0x08,
    0x01, 0xec, 0xe4, 0x68, 0x94, 0x03, 0x42, 0xf1,
    0x73, 0xf1, 0x23, 0xa2, 0x3a, 0xde, 0xe9, 0xf1,
    0xda, 0xc6, 0x54, 0xc4, 0x23, 0x3e, 0x86, 0xea,
    0xcf, 0x6a, 0x3a, 0x33, 0xab, 0xea, 0x9c, 0x04,
    0x14, 0x07, 0x36, 0x06, 0x0b, 0xf9, 0x88, 0x6f,
    0xd5, 0x13, 0xee, 0x29, 0x2b, 0xc3, 0xe4, 0x72,
    0x8d, 0x44, 0xed, 0xd1, 0xac, 0x20, 0x09, 0x2d,
    0xe1, 0xf6, 0xe1, 0x19, 0x05, 0x38, 0xb0, 0x3d,
    0x0f, 0x9f, 0x7f, 0xf8, 0x9e, 0x02, 0xdc, 0x86,
    0x02, 0x86, 0x61, 0x4e, 0x26, 0x5f, 0x5e, 0x9f,
    0x92, 0x1e, 0x0c, 0x24, 0xa4, 0xf5, 0xd0, 0x70,
    0x13, 0xcf, 0x26, 0xc3, 0x43, 0x3d, 0x49, 0x1d,
    0x9e, 0x82, 0x2e, 0x52, 0x5f, 0xbc, 0x3e, 0xc6,
    0x66, 0x29, 0x01, 0x8e, 0x4e, 0x92, 0x2c, 0xbc,
    0x46, 0x75, 0x03, 0x82, 0xac, 0x73, 0xe9, 0xd9,
    0x7e, 0x0b, 0x67, 0xef, 0x54, 0x52, 0x1a
};

static void persistentRefIs(CFDataRef pref, CFDataRef data) {
    CFMutableDictionaryRef dict = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
    CFTypeRef result = NULL;
    CFDictionaryAddValue(dict, kSecValuePersistentRef, pref);
    CFDictionaryAddValue(dict, kSecReturnData, kCFBooleanTrue);
    ok_status(SecItemCopyMatching(dict, &result), "lookup item data by persistent ref");
    ok(CFEqual(data, result), "result %@ equals expected data %@", result, data);
    CFReleaseNull(result);
    CFReleaseNull(dict);
}

/* Test add api in all its variants. */
static void tests(void)
{
    CFDataRef myCertData = NULL;
	SecCertificateRef myCert = NULL;
	CFTypeRef  certHandle = NULL;

    /* Create myCert and setup dict. */
    myCertData = CFDataCreateWithBytesNoCopy(NULL, _myCert, sizeof(_myCert),
        kCFAllocatorNull);
	isnt(myCert = SecCertificateCreateWithData(NULL, myCertData),
		NULL, "create myCert");
    CFMutableDictionaryRef dict = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
    CFDictionaryAddValue(dict, kSecClass, kSecClassCertificate);
    CFDictionaryAddValue(dict, kSecValueRef, myCert);

    /* Case 1: add certificate with no return pointer. */
    ok_status(SecItemAdd(dict, NULL),
        "Case 1: add certificate NULL return pointer");

    /* Case 1a: test add duplicate item */
    is_status(SecItemAdd(dict, NULL), errSecDuplicateItem, "add certificate again");

    ok_status(SecItemDelete(dict), "delete certificate");
    /* Case 1b: add certificate ref should be returned implicitly. */
    ok_status(SecItemAdd(dict, &certHandle),
              "Case 1: add certificate dont ask for return type");
    // See also: "<rdar://problem/7091317&6073493> SecItemAdd unimplemented feature";
    is(certHandle, NULL, "nothing returned");
    CFReleaseNull(certHandle);
    ok_status(SecItemDelete(dict), "delete certificate");

    /* Case 2: add certificate ask for persistent ref. */
    CFDictionaryAddValue(dict, kSecReturnPersistentRef, kCFBooleanTrue);
    ok_status(SecItemAdd(dict, &certHandle),
        "Case 2: add certificate ask for persistent ref");
    is(CFGetTypeID(certHandle), CFDataGetTypeID(), "persistent ref returned");
    /* Check if persistent ref yields cert data again when searched. */
    persistentRefIs(certHandle, myCertData);
    CFReleaseNull(certHandle);
    CFDictionaryRemoveValue(dict, kSecReturnPersistentRef);
    ok_status(SecItemDelete(dict), "delete certificate");

    /* Case 3: add certificate ask for ref to be returned explicitly. */
    CFDictionaryAddValue(dict, kSecReturnRef, kCFBooleanTrue);
    ok_status(SecItemAdd(dict, &certHandle),
        "Case 3: add certificate ask for ref to be returned explicitly");
    ok(CFEqual(myCert, certHandle), "certificate ref matches");
    CFReleaseNull(certHandle);
    CFDictionaryRemoveValue(dict, kSecReturnRef);
    ok_status(SecItemDelete(dict), "delete certificate");

    /* Case 4: add certificate ask for data to be returned explicitly. */
    CFDictionaryAddValue(dict, kSecReturnData, kCFBooleanTrue);
    ok_status(SecItemAdd(dict, &certHandle),
        "Case 4: add certificate ask for data to be returned explicitly");
    ok(CFEqual(myCertData, certHandle), "certificate data matches");
    CFReleaseNull(certHandle);
    CFDictionaryRemoveValue(dict, kSecReturnData);
    ok_status(SecItemDelete(dict), "delete certificate");

    /* Case 5: add certificate ask for attributes to be returned explicitly. */
    CFDictionaryAddValue(dict, kSecReturnAttributes, kCFBooleanTrue);
    ok_status(SecItemAdd(dict, &certHandle),
        "Case 5: add certificate ask for attributes to be returned explicitly");
    is(CFGetTypeID(certHandle), CFDictionaryGetTypeID(), "result is a dict");
    ok(!CFDictionaryContainsKey(certHandle, kSecValueData),
        "result has no data");
    ok(!CFDictionaryContainsKey(certHandle, kSecValueRef),
        "result has no ref");
    ok(!CFDictionaryContainsKey(certHandle, kSecValuePersistentRef),
        "result has no persistent ref");
    /* @@@ Check attribute values. Against this list:
        kSecClassCertificate item attributes:
        kSecAttrAccessGroup
        kSecAttrCertificateType
        kSecAttrCertificateEncoding
        kSecAttrLabel
        kSecAttrSubject
        kSecAttrIssuer
        kSecAttrSerialNumber
        kSecAttrSubjectKeyID
        kSecAttrPublicKeyHash
    */
    is(CFGetTypeID(CFDictionaryGetValue(certHandle, kSecAttrCertificateType)),
        CFNumberGetTypeID(), "certificate type is a number");
    is(CFGetTypeID(CFDictionaryGetValue(certHandle, kSecAttrCertificateEncoding)),
        CFNumberGetTypeID(), "certificate encoding is a number");
    CFReleaseNull(certHandle);
    CFDictionaryRemoveValue(dict, kSecReturnAttributes);
    ok_status(SecItemDelete(dict), "delete certificate");

    /* Case 6: add certificate ask for attributes, data, ref and persistent ref
       to be returned explicitly. */
    CFDictionaryAddValue(dict, kSecReturnData, kCFBooleanTrue);
    CFDictionaryAddValue(dict, kSecReturnAttributes, kCFBooleanTrue);
    CFDictionaryAddValue(dict, kSecReturnRef, kCFBooleanTrue);
    CFDictionaryAddValue(dict, kSecReturnPersistentRef, kCFBooleanTrue);
    ok_status(SecItemAdd(dict, &certHandle),
        "Case 6: add certificate ask for attributes, data, ref and persistent ref to be returned explicitly");
    isnt(certHandle, NULL, "certificate handle returned");
    is(CFGetTypeID(certHandle), CFDictionaryGetTypeID(), "result is a dict");
    ok(CFEqual(myCertData, CFDictionaryGetValue(certHandle, kSecValueData)),
        "certificate data in dict matches");
    ok(CFEqual(myCert, CFDictionaryGetValue(certHandle, kSecValueRef)),
        "certificate ref in dict matches");
    /* Check if persistent ref yields cert data again when searched. */
    persistentRefIs(CFDictionaryGetValue(certHandle, kSecValuePersistentRef),
        myCertData);
    ok(CFEqual(kSecClassCertificate,
        CFDictionaryGetValue(certHandle, kSecClass)),
        "certificate class in dict matches");
    is(CFGetTypeID(CFDictionaryGetValue(certHandle, kSecAttrCertificateType)),
        CFNumberGetTypeID(), "certificate type is a number");
    is(CFGetTypeID(CFDictionaryGetValue(certHandle, kSecAttrCertificateEncoding)),
        CFNumberGetTypeID(), "certificate encoding is a number");
    CFReleaseNull(certHandle);
    CFDictionaryRemoveValue(dict, kSecReturnData);
    CFDictionaryRemoveValue(dict, kSecReturnAttributes);
    CFDictionaryRemoveValue(dict, kSecReturnRef);
    CFDictionaryRemoveValue(dict, kSecReturnPersistentRef);
    ok_status(SecItemDelete(dict), "delete certificate");

    /* Case 7: add certificate with bogus attribute. */
    CFDataRef tagData = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault,
        (const UInt8 *)"funnytag", 8, kCFAllocatorNull);
    CFDictionaryAddValue(dict, kSecAttrApplicationTag, tagData);
    is_status(SecItemAdd(dict, &certHandle), errSecNoSuchAttr,
        "Case 7: add certificate with bogus attribute returns errSecNoSuchAttr");
    CFReleaseNull(certHandle);
    is_status(SecItemDelete(dict), errSecNoSuchAttr,
        "delete certificate with bogus attribute returns errSecNoSuchAttr");
    CFDictionaryRemoveValue(dict, kSecAttrApplicationTag);
    CFReleaseNull(tagData);

    /* Case 8: add certificate with synchronizable attribute. */
    CFDictionaryAddValue(dict, kSecAttrSynchronizable, kCFBooleanTrue);
    ok_status(SecItemAdd(dict, &certHandle),
              "Case 7: add certificate with synchronizable attribute works");
    CFReleaseNull(certHandle);
    ok_status(SecItemDelete(dict),
              "delete certificate with synchronizable attribute works");
    CFDictionaryRemoveValue(dict, kSecAttrSynchronizable);

    /* Cleanup. */
    CFReleaseNull(dict);
    CFReleaseNull(myCert);
    CFReleaseNull(myCertData);

}

int si_05_add(int argc, char *const *argv)
{
	plan_tests(41);

	tests();

	return 0;
}
